<?php
class model_newagk_adminaccessresourc extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'agk_admin_access_resourc';
    }
	
	
	public function getMenu($role_id,$platform_id,$units_id=[0],$typesof=0) {
			if(!$typesof) return false;
			$resourc_id = [0];
			$resourc = $this->select('role_id='.$role_id.' and unit_id in('.implode(',',$units_id).') and typesof='.$typesof, 'resourc_id')->items;
			foreach($resourc as $v){
				$resourc_id[] = $v['resourc_id'];
			}
		
        return $resourc_id;
    }
	
	
	public function resourceSystem($characters,$platform_id,$resourc_id,$adminuserid,$typesof=0,$title='') {
		if(!$typesof) return false;

        if($typesof=='qbank'){ //11题库
			$qbank = new model_newagk_qbank();
			$questionsbinding = new model_newagk_questionsbinding();
			$condition = 'platformId='.$platform_id;
			if($title!='') $condition .= " and name like '%{$title}%'";
			if($characters=='zg'){
				$list = $qbank->select($condition,'id,name,unit_id')->items;
			} else {
				$binding = $questionsbinding->binding($adminuserid);
				$id = array_merge($resourc_id,$binding);
				$list = $qbank->select($condition.' and id in('.implode(',',$id).')','id,name,unit_id')->items;
			}
		/*} else  if($typesof==12){ //12试卷
			if($characters=='zg'){
				
			} else {
				
			}*/

		} else  if($typesof=='basic'){ //13考场
			$basic = new model_newagk_basic();
			$basicbinding = new model_newagk_basicbinding();
			$condition = 'platformId='.$platform_id;
			if($title!='') $condition .= " and name like '%{$title}%'";
			if($characters=='zg'){
				$list = $basic->select($condition,'id,basic name,unit_id')->items;
			} else {
				$binding = $basicbinding->binding($adminuserid);
				$id = array_merge($resourc_id,$binding);
				$list = $basic->select($condition.' and id in('.implode(',',$id).')','id,basic name,unit_id')->items;
			}
		} else  if($typesof=='attendance'){ 
			/**测试数据**/
			$basic = new model_newagk_basic();
				$list = $basic->select($condition,'id,basic name,unit_id unitId')->items;
			
		
		}
		$datas = [];
		if($characters=='zg'){
			foreach($list as $vr){
					$datas[] = [
						'id'=>$vr['id'],
						'title'=>$vr['name'],
						'unit_id'=>$vr['unit_id'],
						'operating'=>1, //1可操作2不可操作
						'expand' =>in_array($vr['id'],$resourc_id) ? 1 : 2,
					];
				}
		} else {
			foreach($list as $vr){
					$datas[] = [
						'id'=>$vr['id'],
						'title'=>$vr['name'],
						'unit_id'=>$vr['unit_id'],
						'operating'=>in_array($vr['id'],$binding) ? 1 : 2, //1可操作2不可操作
						'expand' =>in_array($vr['id'],$resourc_id) ? 1 : 2,
					];
				}
		}
				
				
		
		return $list;
    }
	
	public function individual($typesof=0,$userid,$resources) {
		if(!$typesof) return false;

        if($typesof=='qbank'){ //11题库

			$questionsbinding = new model_newagk_questionsbinding();
			$questionsbinding->delete('adminUserId='.$userid);
			foreach($resources as $vr){
				$insert_all = [
					"adminUserId" => $userid,
					"bankid" => $vr,
					];
				$adminaccessresourc->insert($insert_all, true);
				usleep(100);
			}
			
			
		/*} else  if($typesof==12){ //12试卷
			if($characters=='zg'){
				
			} else {
				
			}*/

		} else  if($typesof=='basic'){ //13考场

			$basicbinding = new model_newagk_basicbinding();
			$basicbinding->delete('adminUserId='.$userid);
			foreach($resources as $vr){
				$insert_all = [
					"adminUserId" => $userid,
					"basicid" => $vr,
					];
				$adminaccessresourc->insert($insert_all, true);
				usleep(100);
			}
			
		}

    }
	
	 
	
	
}
